home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / old-stream / ostream.cc < prev    next >
C/C++ Source or Header  |  1992-01-17  |  4KB  |  220 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1989 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. /* *** Version 1.2 -- nearly 100% AT&T 1.2 compatible *** */
  20.  
  21. #ifdef __GNUG__
  22. #pragma implementation
  23. #endif
  24. #include <stream.h>
  25. #include <stdarg.h>
  26. #include <values.h>
  27. #include <ctype.h>
  28. #include <Obstack.h>
  29.  
  30. ostream::ostream(streambuf* s)
  31.      : bp(s), state(_good), ownbuf(0) {}
  32.  
  33. ostream::ostream(int sz, char* buf)
  34.      : state(_good), ownbuf(1)
  35. {
  36.   if (buf == 0) 
  37.   {
  38.     buf = new char[sz];
  39.     bp = new streambuf(buf, sz);
  40.     bp->setbuf(buf, sz);
  41.     bp->alloc = 1;
  42.   }
  43.   else
  44.   {
  45.     bp = new streambuf(buf, sz);
  46.     bp->alloc = 0;
  47.   }
  48. }
  49.  
  50.  
  51. ostream::ostream(const char* filename, io_mode m, access_mode a)
  52.      : state(_good), ownbuf(1)
  53. {
  54.   bp = new Filebuf(filename, m, a);
  55. }
  56.  
  57. ostream::ostream(const char* filename, const char* m)
  58.      : state(_good), ownbuf(1)
  59. {
  60.   bp = new Filebuf(filename, m);
  61. }
  62.  
  63. ostream::ostream(int filedesc, io_mode m)
  64.      : state(_good), ownbuf(1)
  65. {
  66.   bp = new Filebuf(filedesc, m);
  67. }
  68.  
  69. ostream::ostream(FILE* fileptr)
  70.      : state(_good), ownbuf(1)
  71. {
  72.   bp = new Filebuf(fileptr);
  73. }
  74.  
  75. ostream::ostream(int filedesc)
  76.      : state(_good), ownbuf(1)
  77. {
  78.   bp = new filebuf(filedesc);
  79. }
  80.  
  81. ostream::ostream(int filedesc, char* buf, int buflen)
  82.      : state(_good), ownbuf(1)
  83. {
  84.   bp = new filebuf(filedesc, buf, buflen);
  85. }
  86.  
  87. ostream::~ostream()
  88. {
  89.   if (ownbuf) delete bp;
  90. }
  91.  
  92. ostream&  ostream::open(const char* filename, io_mode m, access_mode a)
  93. {
  94.   return failif(bp->open(filename, m, a) == 0);
  95. }
  96.  
  97. ostream&  ostream::open(const char* filename, const char* m)
  98. {
  99.   return failif(bp->open(filename, m) == 0);
  100. }
  101.  
  102. ostream&  ostream::open(int  filedesc, io_mode m)
  103. {
  104.   return failif(bp->open(filedesc, m) == 0);
  105. }
  106.  
  107. ostream&  ostream::open(FILE* fileptr)
  108. {
  109.   return failif(bp->open(fileptr) == 0);
  110. }
  111.  
  112. ostream&  ostream::open(const char* filenam, open_mode m)
  113. {
  114.   return failif(bp->open(filenam, m) == 0);
  115. }
  116.  
  117. ostream& ostream::form(const char* fmt...)
  118. {
  119.   va_list args;
  120.   va_start(args, fmt);
  121.   char buf[BUFSIZ];
  122. #ifndef HAVE_VPRINTF
  123.   FILE b;
  124.   b._flag = _IOWRT|_IOSTRG;
  125.   b._ptr = buf;
  126.   b._cnt = BUFSIZ;
  127.   _doprnt(fmt, args, &b);
  128.   putc('\0', &b);
  129. #else
  130.   vsprintf(buf, fmt, args);
  131. #endif
  132.   va_end(args);
  133.   return put(buf);
  134. }
  135.  
  136. ostream& ostream::operator<<(short  n)
  137.   return put(itoa(long(n)));
  138. }
  139.  
  140. ostream& ostream::operator<<(unsigned short n)
  141.   return put(itoa((unsigned long)(n)));
  142. }
  143.  
  144. ostream& ostream::operator<<(int    n)
  145.   return put(itoa(long(n)));
  146. }
  147.  
  148. ostream& ostream::operator<<(unsigned int n)
  149.   return put(itoa((unsigned long)(n)));
  150. }
  151.  
  152. ostream& ostream::operator<<(long   n)
  153.   return put(itoa(n));
  154. }
  155.  
  156. ostream& ostream::operator<<(unsigned long n)
  157.   return put(itoa(n));
  158. }
  159.  
  160. #ifdef __GNUG__
  161. #ifndef VMS
  162. ostream& ostream::operator<<(long long n)
  163.   return put(itoa(n));
  164. }
  165.  
  166. ostream& ostream::operator<<(unsigned long long n)
  167.   return put(itoa(n));
  168. }
  169. #endif
  170. #endif
  171. ostream& ostream::operator<<(float  n)
  172.   return put(dtoa(double(n)));
  173. }
  174.  
  175. ostream& ostream::operator<<(double n)
  176.   return put(dtoa(n));
  177. }
  178.  
  179. ostream& ostream::operator<<(const void* p)
  180.   put("0x");
  181.   return put(itoa((unsigned long)(p), 16));
  182. }
  183.  
  184.  
  185. const char* ostream::name()
  186. {
  187.   return bp->name();
  188. }
  189.  
  190. void ostream::error()
  191. {
  192.   bp->error();
  193. }
  194.  
  195. #ifndef DEFAULT_filebuf
  196.  
  197. ostream  cerr(stderr);
  198. ostream  cout(stdout);
  199.  
  200. #else
  201.  
  202. static char cerrbuf[1];
  203. static char coutbuf[BUFSIZE];
  204.  
  205. ostream cerr(2, cerrbuf, 1);
  206. ostream cout(1, coutbuf, BUFSIZE);
  207.  
  208. #endif
  209.